package role_dal

import (
	"gorm.io/gorm"
	"starry_sky/internal/dal"
	"starry_sky/internal/model"
)

// ListRolesByUser 通过user得到所有角色信息
func ListRolesByUser(user *model.User, roles *[]*model.Role) error {
	err := dal.DB.Model(&user).Association("Roles").Find(&roles)
	if err != nil {
		return err
	}
	return nil
}

// 清除角色权限
func ClearRoleAndPermission(db *gorm.DB, role *model.Role) error {
	err := db.Model(&role).Association("Permissions").Clear()
	if err != nil {
		return err
	}
	return nil
}

// 绑定角色权限
func BindRoleAndPermission(db *gorm.DB, role *model.Role, permissions []model.Permission) error {
	err := db.Model(&role).Association("Permissions").Append(permissions)
	if err != nil {
		return err
	}
	return nil
}
